﻿package bipede.animation
{
	import flash.events.TimerEvent;
	import drawer.events.DrawEvent;
	import events.MoveEvent;
	import fab.geom.Bezier;
	import flash.geom.Point;

	public class Posture extends AnimationTimer
	{
		private var cb_curve_droite:Bezier;
		private var cb_curve_gauche:Bezier;
		
		public function Posture( _type:String )
		{
			type = _type;
			super();
		}
		override public function start():void
		{
			avancement = 0;
			
			switch( type )
			{
				case "bras_ballants" :
				
					cb_curve_droite = new Bezier( cfg.bz_droite );
					cb_curve_gauche = new Bezier( cfg.bz_gauche );
					dispatchEvent( new DrawEvent( DrawEvent.DRAWCURVE, cb_curve_droite, 0xFF0000 ) );
					dispatchEvent( new DrawEvent( DrawEvent.DRAWCURVE, cb_curve_gauche, 0xFF0000 ) );
			
				break;
			}			
			super.start();	
		}	
		override public function _tick( event:TimerEvent ):void
		{
			avancement += cfg.vitesse;

			dispatchEvent( new MoveEvent( MoveEvent.MOVE, new Point( 0, cfg.amplitude * sinus_phase( avancement, cfg.phase ) ), cfg.support, true ) );
			dispatchEvent( new MoveEvent( MoveEvent.MOVEMAINDROITE, cb_curve_droite.pointAt( .5 + sinus_phase( avancement, cfg.phase*2.6 ) / 2 ), null ) );
			dispatchEvent( new MoveEvent( MoveEvent.MOVEMAINGAUCHE, cb_curve_gauche.pointAt( .5 + sinus_phase( avancement, cfg.phase*2.3 ) / 2 ), null ) );
			dispatchEvent( new MoveEvent( MoveEvent.POSEPIEDGAUCHE ) );
			dispatchEvent( new MoveEvent( MoveEvent.POSEPIEDDROIT ) );
		}
		public function sinus_phase( _x:Number, phase:Number ):Number
		{
			return Math.sin( 2 * Math.PI * ( _x % phase ) / phase );
		}		
	}
}